﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using CMS.Web;
using CMS.Model;

namespace CMS.Web.Controllers.Admin
{
    public class AdminBaseController : Controller
    {

        public readonly UserManager<User> _userManager;

        public async Task<User> CurrentUser()
        {
            return await _userManager.GetUserAsync(HttpContext.User);
        }

        public AdminBaseController()
        {
            jsonResponse = new JsonResponse();
        }
        public AdminBaseController(UserManager<User> userManager)
        {
            jsonResponse = new JsonResponse();
            _userManager = userManager;
        }

        /// <summary>
        /// 请求返回对象
        /// </summary>
        public JsonResponse jsonResponse { get; set; }

        /// <summary>
        /// 获取IP
        /// </summary>
        /// <returns></returns>
        public string GetRequestIP()
        {
            var ip = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrEmpty(ip))
            {
                ip = HttpContext.Connection.RemoteIpAddress.ToString();
            }
            return ip;
        }
    }
}
